<?php
namespace app\admin\controller;
use think\Controller;
use think\Request;
use think\Session;
use think\Db;

class Admin extends Controller
{   
    protected $module;
    protected $controller;

   public function initialize()
   {
   		parent::__construct();
       //判断session是否存在
       if(empty(session('adminname'))){
         $this->redirect('admin/login/login');
       }

       //获取当前的模块
        $module = request()->action();
       //获取模块下的控制器
        $controller = request()->controller();


        // var_dump($module,$controller);
        $name = Session::get('adminname');
        if ($name !='chen') {
           $controller.'/'.$module;
           $list= Db::table('admininfo')
                      ->alias('a')
                      ->join('tb_admin_role ar','a.id = ar.uid')
                      ->join('tb_role r', 'ar.rid = r.id')
                      ->join('tb_role_authorization ra','r.id = ra.rid')
                      ->join('tb_authorization ta', 'ra.aid = ta.id')
                      ->where('adminname',$name)
                      ->field('ta.id as id,r.id as rid, ta.authorization, ta.control, ta.method')
                      ->order('ta.id asc')
                      ->select();
           // var_dump($list) ;
           $i = 0;
           foreach ($list as $k => $v) {
                 if ($controller==$v['control']&&$module==$v['method']) {
                      $i = 1;
                 }
           }

           if ($i != 1) {
             $this->error('无此权限');
           }
            
        }

   }


}